/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.ecarx.systemui.plugin.AppConstants;
import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.controller.hvac.FanSignalController;
import com.ecarx.systemui.plugin.ui.custom.hvac.AutoFanControlView;
import com.ecarx.systemui.plugin.ui.custom.hvac.HandFanControlView;
import com.ecarx.systemui.plugin.ui.dialog.base.AutoCloseDialog;
import com.ecarx.systemui.plugin.model.listener.FanFuncNotifyViewListener;
import com.ecarx.systemui.plugin.model.listener.HvacModeListener;
import com.ecarx.systemui.plugin.utils.DensityUtils;
import com.ecarx.systemui.plugin.utils.LogUtils;
import com.ecarx.xui.adaptapi.FunctionStatus;

public class FanControlDialog extends AutoCloseDialog implements FanFuncNotifyViewListener {

    private static final String TAG = "FanControlDialog";
    private AutoFanControlView mAutoFanControlView;
    private HandFanControlView mHandFanControlView;

    private boolean isAutoMode;

    private FanSignalController mFanSignalController;

    public FanControlDialog(@NonNull Context context) {
        super(context);
    }

    public FanControlDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        setContentView(R.layout.dialog_fan_speed_control);

        initConfig();

        mFanSignalController = FanSignalController.getInstance();

        mAutoFanControlView = findViewById(R.id.auto_fan_view);
        mHandFanControlView = findViewById(R.id.hand_fan_view);
    }

    HvacModeListener hvacModeListener = new HvacModeListener() {
        @Override
        public void onHvacModeChange(boolean isAuto) {
            isAutoMode = isAuto;
            setFanMode(isAuto);
        }
    };

    private void initConfig() {
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.WRAP_CONTENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.type = AppConstants.PLUGIN_DIALOG_TYPE;
            params.gravity = Gravity.START;
            params.dimAmount = 0;
            params.y = DensityUtils.getScreenHeight() - DensityUtils.dp2px(94);
            window.setAttributes(params);
        }
    }


    /**
     * set the hvac mode
     *
     * @param isAuto is auto
     */
    public void setFanMode(boolean isAuto) {
        LogUtils.d(TAG, "setFanMode: " + isAuto);
        isAutoMode = isAuto;
        mAutoFanControlView.setVisibility(isAuto ? View.VISIBLE : View.GONE);
        mHandFanControlView.setVisibility(isAuto ? View.GONE : View.VISIBLE);
    }

    @Override
    public void show() {
        super.show();
        if (mFanSignalController != null) {
            boolean mode = mFanSignalController.getFanModeSwitch();
            isAutoMode = mode;
            LogUtils.d(TAG, "isAutoFanMode:" + isAutoMode);
            setFanMode(isAutoMode);

            mFanSignalController.setHvacModeListener(hvacModeListener);
            mFanSignalController.setFanSignalNotifyDialogListener(this);
        } else {
            LogUtils.e(TAG, "show(), but mFanSignalController is null");
        }

    }

    @Override
    public void dismiss() {
        super.dismiss();
        mFanSignalController.setHvacModeListener(null);
        mFanSignalController.setFanSignalNotifyDialogListener(null);
    }


    @Override
    public void onHvacModeChange(boolean isAuto, FunctionStatus functionStatus) {
        LogUtils.d(TAG, "onHvacModeChange isAutoMode:" + isAutoMode + "  functionStatus:" + functionStatus);
        isAutoMode = isAuto;
        setFanMode(isAuto);
    }

    @Override
    public void onHvacHandFanChange(Integer value, FunctionStatus functionStatus) {
        LogUtils.d(TAG, "onHvacHandFanChange isAutoMode:" + isAutoMode + "  value:" + value + "  functionStatus:" + functionStatus);
        if (functionStatus == FunctionStatus.active) {
            mHandFanControlView.updateProgress(value);
        }
    }

    @Override
    public void onHvacAutoFanChange(Integer value, FunctionStatus functionStatus) {
        LogUtils.d(TAG, "onHvacAutoFanChange isAutoMode:" + isAutoMode + "  value:" + value + "  functionStatus:" + functionStatus);
        if (functionStatus == FunctionStatus.active) {
            mAutoFanControlView.updateProgress(value);
        }
    }
}
